package me.jessyan.mvparms.demo.di.module.coupon;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.CustomPopupWindow;

import javax.inject.Named;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import dagger.Module;
import dagger.Provides;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.mvp.contract.coupon.CouponDetailContract;
import me.jessyan.mvparms.demo.mvp.model.coupon.CouponDetailModel;
import me.jessyan.mvparms.demo.utils.PerfectClickListener;


@Module
public class CouponDetailModule {
    private CouponDetailContract.View view;

    /**
     * 构建CouponDetailModule时,将View的实现类传进来,这样就可以提供View的实现类给presenter
     *
     * @param view
     */
    public CouponDetailModule(CouponDetailContract.View view) {
        this.view = view;
    }

    @ActivityScope
    @Provides
    CouponDetailContract.View provideCouponDetailView() {
        return this.view;
    }

    @ActivityScope
    @Provides
    CouponDetailContract.Model provideCouponDetailModel(CouponDetailModel model) {
        return model;
    }

    @ActivityScope
    @Provides
    @Named("sharePop")
    CustomPopupWindow sharePop(){
        View content = LayoutInflater.from(view.getActivity())
                .inflate(R.layout.pop_coupon_detail_delete,null);
        CustomPopupWindow popupWindow = CustomPopupWindow.builder()
                .isFocus(true)
                .isOutsideTouch(true)
                .isWrap(true)
                .contentView(content)
                .defultHeight(ArmsUtils.dip2px(view.getActivity().getApplication(),96))

                .customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {
                        contentView.findViewById(R.id.delete_tv).setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {

                                view.dismissPop();
                                view.popBackDelete();
                            }
                        });
                        contentView.findViewById(R.id.share_coupon_tv).setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                view.dismissPop();
                                view.popBackShare();
                            }
                        });
                    }
                }).build();
        //PopupWindow全屏显示
        popupWindow.setClippingEnabled(false);
        return popupWindow;
    }

    @ActivityScope
    @Provides
    @Named("deletePop")
    CustomPopupWindow deletePop(){
        View content = LayoutInflater.from(view.getActivity())
                .inflate(R.layout.pop_gift_detail_delete,null);
        CustomPopupWindow popupWindow = CustomPopupWindow.builder()
                .isFocus(true)
                .isOutsideTouch(true)
                .isWrap(true)
                .contentView(content)
                .defultHeight(ArmsUtils.dip2px(view.getActivity().getApplication(),56))

                .customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {
                        contentView.findViewById(R.id.delete_tv).setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {

                                view.dismissPop();
                                view.popBackDelete();
                            }
                        });
                    }
                }).build();
        //PopupWindow全屏显示
        popupWindow.setClippingEnabled(false);
        return popupWindow;
    }

    @ActivityScope
    @Provides
    @Named("sureDeletePop")
    CustomPopupWindow sureDeletePop(){

        View content = LayoutInflater.from(view.getActivity())
                .inflate(R.layout.pop_frag_mine_loginout,null);
        CustomPopupWindow popupWindow = CustomPopupWindow.builder()
                .isFocus(true)
                .isOutsideTouch(true)
                .isWrap(true)
                .contentView(content)
                .customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {
                        TextView titleTv= contentView.findViewById(R.id.pop_title_tv);
                        TextView startTv = contentView.findViewById(R.id.pop_start_tv);
                        TextView endTv = contentView.findViewById(R.id.pop_end_tv);
                        titleTv.setText("确定要删除这张优惠券？");
                        startTv.setText("删除");

                        startTv.setOnClickListener(new PerfectClickListener() {

                            @Override
                            protected void onNoDoubleClick(View v) {

                                view.dismissPop();
                                view.popBackSureDelete();
                            }
                        });
                        endTv.setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                view.dismissPop();
                            }
                        });

                    }
                }).build();
        //PopupWindow全屏显示
        popupWindow.setClippingEnabled(false);
        return popupWindow;
    }

    @ActivityScope
    @Provides
    @Named("giftCardPop")
    CustomPopupWindow giftCardPop(){

        View content = LayoutInflater.from(view.getActivity())
                .inflate(R.layout.pop_gift_card,null);
        CustomPopupWindow popupWindow = CustomPopupWindow.builder()
                .isFocus(true)
                .isOutsideTouch(true)
                .isWrap(true)
                .contentView(content)
                .customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                        TextView startTv = contentView.findViewById(R.id.pop_start_tv);
                        TextView endTv = contentView.findViewById(R.id.pop_end_tv);
                        EditText phoneEt= contentView.findViewById(R.id.phone_et);

                        startTv.setOnClickListener(new PerfectClickListener() {

                            @Override
                            protected void onNoDoubleClick(View v) {

                                view.dismissPop();
                                if(!ArmsUtils.isEmpty(phoneEt.getText())){
                                    view.popBackGiftCard(phoneEt.getText().toString());
                                }

                            }
                        });
                        endTv.setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                view.dismissPop();
                            }
                        });

                    }
                }).build();
        //PopupWindow全屏显示
        popupWindow.setClippingEnabled(false);
        return popupWindow;
    }

    @ActivityScope
    @Provides
    @Named("QRCodePop")
    CustomPopupWindow QRCodePop(){

        View content = LayoutInflater.from(view.getActivity())
                .inflate(R.layout.pop_coupon_qr_code,null);
        CustomPopupWindow popupWindow = CustomPopupWindow.builder()
                .isFocus(true)
                .isOutsideTouch(true)
                .isWrap(true)
                .contentView(content)
                .customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {


                        TextView endTv = contentView.findViewById(R.id.back_tv);
                        ImageView qrCodeIv = contentView.findViewById(R.id.qr_code_iv);
                        Observable.just("https://www.baidu.com")
                                .subscribeOn(Schedulers.io())
                                .map(new Function<String, Bitmap>() {
                                    @Override
                                    public Bitmap  apply(String s) throws Exception {
                                        return QRCodeEncoder.syncEncodeQRCode(s, ArmsUtils.dip2px(view.getActivity().getApplicationContext(),192));
                                    }
                                })
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Bitmap >() {
                                    @Override
                                    public void accept(Bitmap  o) throws Exception {
                                        qrCodeIv.setImageBitmap(o);
                                    }
                                });
                        endTv.setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                view.dismissPop();
                            }
                        });

                    }
                }).build();
        //PopupWindow全屏显示
        popupWindow.setClippingEnabled(false);
        return popupWindow;
    }
}